package com.ctvit.struts.action;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.ctvit.framework.security.LdapPasswordEncoder;
import com.ctvit.framework.struts2.ActionSupport;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.sms.platform.domain.Login;
import com.ctvit.sms.platform.service.AppOnlyService;

public class AppOnlyAction extends ActionSupport {
	private AppOnlyService appOnlyService = ContextHolder.getBean(AppOnlyService.class);
	private HashMap loginResJoson = null;
	private List msgListJoson = null;
	boolean validFail = false;

	public String receiveMsg() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String userid = request.getParameter("userid");
		String content = request.getParameter("content");
		String title = request.getParameter("title");
		if (StringUtils.isEmpty(title))
			title = "";
		if (StringUtils.isEmpty(content))
			content = "";
		try {
			// content=new String(content.getBytes("ISO-8859-1"),"GBK");
			// title=new String(title.getBytes("ISO-8859-1"),"GBK");
		} catch (Exception e) {
			// TODO: handle exception
		}
		appOnlyService.saveMsg(userid, content, title);
		return "succRecv";
	}

	public String userLogin() throws Exception {
		loginResJoson = new HashMap();
		HttpServletRequest request = ServletActionContext.getRequest();
		String userId = request.getParameter("userId");
		String clientId = request.getParameter("clientId");
		String remoteIP = request.getRemoteAddr();
		String md5Password = request.getParameter("md5Password");
		String saltMd5Password = request.getParameter("clientPwd");
		String sessionToken = request.getParameter("sessionToken");
		HashMap userMap = appOnlyService.doUserLogin(userId, remoteIP, md5Password, saltMd5Password, sessionToken, clientId);
		loginResJoson = userMap;
		return "longinResult";
	}

	/*
	 * 查询人员信息
	 */
	public String searchPerson() throws UnsupportedEncodingException, Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String personNm = request.getParameter("personNm");
		String deptNm = request.getParameter("deptNm");
		String pageRowNum = request.getParameter("pageRowNum");
		String pageSize = request.getParameter("pageSize");
		String userId = request.getParameter("userId");
		// personNm = new String(personNm.getBytes("ISO-8859-1"),"UTF-8");
		// deptNm = new String(deptNm.getBytes("ISO-8859-1"),"UTF-8");
		msgListJoson = appOnlyService.searchPerson(personNm, deptNm, pageRowNum, pageSize,userId);
		return "searPerson";
	}

	public String worderror() throws UnsupportedEncodingException, Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String error = request.getParameter("error");
		String right = request.getParameter("right");
		// error = new String(error.getBytes("ISO-8859-1"),"UTF-8");
		// right = new String(right.getBytes("ISO-8859-1"),"UTF-8");
		msgListJoson = appOnlyService.getWorderror(error, right);
		return "getWorderror";
	}

	public String getWordErrorDoc() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getWorderrorDoc();
		return "WorderrorDoc";
	}

	public String getWorderrorBianXi() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String words = request.getParameter("words");
		//words = new String(words.getBytes("ISO-8859-1"), "UTF-8");
		msgListJoson = appOnlyService.getWorderrorBianXi(words);
		return "WorderrorBianXi";
	}

	public String getWorderPinYin() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.updateWorrderBianxiPinyin();
		return "WorderPinYin";
	}

	public String icharts() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getIcharts();
		return "getIcharts";
	}

	public String chineseToPinYin() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.doChineseToPinYin();
		return "doChineseToPinYin";
	}

	public String getMsgSum() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = null;
		HttpServletRequest request = ServletActionContext.getRequest();
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		String clientId = request.getParameter("clientId");
		// String isNotice = request.getParameter("isNotice");
		// if (StringUtils.equals(isNotice, "1")){
		msgListJoson = appOnlyService.doGetMessage(userId, password, clientId, null);
		// }else{
		// msgListJoson = appOnlyService.doGetMsgSum(userId, password,
		// clientId,null);
		// }
		return "msgSum";
	}

	public String getMsgList() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = null;
		HttpServletRequest request = ServletActionContext.getRequest();
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		String clientId = request.getParameter("clientId");
		String isNotice = request.getParameter("isNotice");
		if (StringUtils.equals(isNotice, "1")) {
			System.out.println("-----------查询消息-----------------11--");
			msgListJoson = appOnlyService.doGetNoticeSum(userId, password, clientId, "1");
		} else {
			System.out.println("-----------查询消息------------------22-");
			msgListJoson = appOnlyService.doGetMsgSum(userId, password, clientId, "1");
		}
		return "msgSum";
	}

	/**
	 * 
	 * @ps:获取内容√
	 * @return
	 */
	public String getMenu() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String funType = request.getParameter("funType");
		msgListJoson = appOnlyService.getMenuList(funType);
		return "MenuList";
	}

	/**
	 * 
	 * @ps:获取节目单子表部分内容√
	 * @return
	 */
	public String getProlist() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String playDate = request.getParameter("playDate");// 获取参数
		String chanCode = request.getParameter("chanlCd");
		msgListJoson = appOnlyService.getProList(playDate, chanCode);
		return "ProList";
	}

	/**
	 * 
	 * @ps:获取频道信息√
	 * @return
	 */
	public String getChannl() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getChannlList();
		return "ChannlList";
	}

	/**
	 * 
	 * @ps:电话√
	 * @return
	 */
	public String getServicePhone() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getServicePhoneList();
		return "ServicePhone";
	}

	/**
	 * 
	 * @ps:表t_tel_way的内容√
	 * @return
	 */
	public String getContactList() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getContactList();
		return "ContactList";
	}

	/**
	 * 
	 * @ps:获取工作日报的内容√
	 * @return
	 */
	public String getGongZuoRiBao() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getGongZuoRB();
		return "GongZuoRiBao";
	}

	public String searchJmdm() throws UnsupportedEncodingException, Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String jmdm = request.getParameter("jmdm");
		String jmmc = request.getParameter("jmmc");
		// jmmc = new String(jmmc.getBytes("ISO-8859-1"), "UTF-8");// 针对中文的转码
		String lmdm = request.getParameter("lmdm");
		String userid = request.getParameter("userid");
		msgListJoson = appOnlyService.searchJmdm(jmdm, jmmc, lmdm, userid);
		return "Jmdm";
	}

	/**
	 * 
	 * @ps:对数据库表t_MyConcern进行操作
	 * @return
	 */
	//取消关注
	public String myConcern() throws Exception {
		System.out.println("--添加关注-----");
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String f_procode = request.getParameter("f_procode");
		String f_user = request.getParameter("f_user");
		String f_opera = request.getParameter("f_opera");
		String f_cap_ver = request.getParameter("f_cap_ver");
		String f_aud_ver = request.getParameter("f_aud_ver");
		msgListJoson = appOnlyService.updateMyConcern(f_procode, f_user, f_opera, f_cap_ver, f_aud_ver);
		return "myConcern";
	}

	//添加关注
	public String myConcernAdd() throws Exception {
		System.out.println("--添加关注-----");
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String f_procode = request.getParameter("f_procode");
		String f_user = request.getParameter("f_user");
		String f_opera = request.getParameter("f_opera");
		String f_cap_ver = request.getParameter("f_cap_ver");
		String f_aud_ver = request.getParameter("f_aud_ver");
		msgListJoson = appOnlyService.updateMyConcernAdd(f_procode, f_user, f_opera, f_cap_ver, f_aud_ver);
		return "myConcernAdd";
	}
	
	
	
	/**
	 * 
	 * @ps:查询表 zcdm_t_subj中的内容
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String getSubj() throws UnsupportedEncodingException, Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String subjCode = request.getParameter("subjCode");
		String channCode = request.getParameter("channCode");
		String subjName = request.getParameter("subjName");
		// subjName = new String(subjName.getBytes("ISO-8859-1"), "UTF-8");
		msgListJoson = appOnlyService.getSubj(subjCode, channCode, subjName);
		return "Subj";
	}

	public String getSys() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getSys();
		return "Sys";
	}

	public String getMyConfig() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String userId = request.getParameter("userid");
		msgListJoson = appOnlyService.getMyConfig(userId);
		return "MyConfig";
	}

	public String setMyConfig() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String userId = request.getParameter("userId");
		String timeFrom = request.getParameter("timeFrom");
		String timeTo = request.getParameter("timeTo");
		String channel = request.getParameter("channel");
		String cha = request.getParameter("CHA");
		String sub = request.getParameter("SUB");
		String sys = request.getParameter("SYS");
		msgListJoson = appOnlyService.updateMyConfig(userId, timeFrom, timeTo, channel, cha, sub, sys);
		return "setMyConfig";
	}

	public String setMyConfigOth() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String flUserid = request.getParameter("flUserid");
		String flType = request.getParameter("flType");
		String flValue = request.getParameter("flValue");
		String flState = request.getParameter("flState");
		msgListJoson = appOnlyService.updateMyConfigOth(flUserid, flType, flValue, flState);
		return "setMyConfigOth";
	}

	public String getSubscription() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String userid = request.getParameter("userid");
		msgListJoson = appOnlyService.getSubscription(userid);
		return "Subscription";
	}

	public String getProgState() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String userid = request.getParameter("userid");
		msgListJoson = appOnlyService.getProgState(userid);
		return "ProgState";
	}

	public String getMeeting() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		msgListJoson = appOnlyService.getMeeting();
		return "Meeting";
	}

	public HashMap getLoginResJoson() {
		return loginResJoson;
	}

	public void setLoginResJoson(HashMap loginResJoson) {
		this.loginResJoson = loginResJoson;
	}

	public List getMsgListJoson() {
		return msgListJoson;
	}

	public void setMsgListJoson(List msgListJoson) {
		this.msgListJoson = msgListJoson;
	}

	@Override
	public void validate() {
		super.validate();
		HttpServletRequest request = ServletActionContext.getRequest();
		String accessToken = request.getParameter("accessToken");
		String userId = request.getParameter("userId");
		Login login = appOnlyService.getUserLoginInfo(userId);
		String saltAccessToken = "";
		validFail = false;
		if (login != null) {
			LdapPasswordEncoder ldapPasswordEncoder = ContextHolder.getBean("passwordEncoder");
			saltAccessToken = ldapPasswordEncoder.encodePassword(login.getZAccessToken(), login.getZSalt().getBytes());
			if (login.getZAccessExpired().before(new Timestamp(Calendar.getInstance().getTimeInMillis()))) {
				validFail = true;
			} else if (!saltAccessToken.equals(accessToken)) {
				validFail = true;
			}
		} else {
			validFail = true;
		}
//		validFail = false;
	}
	
	//保存通讯录消息
	public String saveRemind() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		System.out.println("---保存消息提醒-----");
		HttpServletRequest request = ServletActionContext.getRequest();
		String title = request.getParameter("title");//标题
		String startTime = request.getParameter("startTime");//开始时间
		String endTime = request.getParameter("endTime");//结束时间
		String note = request.getParameter("note");//备注
		String type = request.getParameter("type");//提醒方式，0：不提醒，1：提前半小时，2：提前一小时，3：提前2小时，4：提前6小时
		String userId = request.getParameter("userId");//用户ID
		
		String remind_id = request.getParameter("remind_id");//主键，如果主键值不为空，则说明是修改
		
		msgListJoson = appOnlyService.saveRemind(title, startTime, endTime, note,type,userId,remind_id);
		
		return "saveRemind";
	}
	
	//删除消息，逻辑删除
	public String updateRemind() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		System.out.println("---删除消息提醒-----");
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");//主键
		String userId = request.getParameter("userId");//人员ID
		
		msgListJoson = appOnlyService.updateRemind(id,userId);
		
		return "updateRemind";
		
	}
	
	//查询我的关注对应频道的栏目
	public String getSubjList() throws Exception{
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String chanlCode = request.getParameter("chanlCode");//频道代码
		System.out.println("查询栏目--频道代码-----"+chanlCode);
		msgListJoson = appOnlyService.getSubjList(chanlCode);
		
		return "getSubjList";
	}
	
	/**
	 * 
	 * @ps:根据栏目获取节目单子表部分内容√
	 * @return
	 */
	public String getSubjProlist() throws Exception {
		if (validFail) {
			throw new Exception("Acccess Authetication Failed!");
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String subjCode = request.getParameter("subjCode");
		String userId = request.getParameter("userId");
		msgListJoson = appOnlyService.getSubjProlist(subjCode,userId);
		return "ProList";
	}
}
